#include "stdafx.h"
#include "MyControls/SegmentCommands.h"
#include "MyControls/Segment.h"
#include "MyControls/Sequence.h"
#include "MyControls/Resource.h"


namespace My {

BEGIN_MESSAGE_MAP(SegmentCommands, CCmdTarget)
	ON_COMMAND(ID_SEGMENT_PROCESS, OnProcess)
	ON_UPDATE_COMMAND_UI(ID_SEGMENT_PROCESS, OnUpdateProcess)
	ON_COMMAND(ID_SEGMENT_CANCEL, OnCancel)
	ON_UPDATE_COMMAND_UI(ID_SEGMENT_CANCEL, OnUpdateCancel)

	ON_COMMAND(ID_SEGMENT_SET_BACK, OnSetBack)
	ON_UPDATE_COMMAND_UI(ID_SEGMENT_SET_BACK, OnUpdateSetBack)
	ON_COMMAND(ID_SEGMENT_SET_FRONT, OnSetFront)
	ON_UPDATE_COMMAND_UI(ID_SEGMENT_SET_FRONT, OnUpdateSetFront)

END_MESSAGE_MAP()

void SegmentCommands::OnProcess()
{
	GetSegment()->StartProcessing();
}

void SegmentCommands::OnUpdateProcess(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(GetSegment()->NeedsProcessing());
}

void SegmentCommands::OnCancel()
{
	GetSegment()->CancelProcessing(false);
}

void SegmentCommands::OnUpdateCancel(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(GetSegment()->IsProcessed());
}

////////////////////////////
// Set Segment Back/Front //
////////////////////////////

void SegmentCommands::OnSetBack() 
{
	Span t = GetSegment()->GetTimeSpan();
	t.first = GetSequence()->GetCurrentFrame();
	GetSegment()->SetTimeSpan(t);
}

void SegmentCommands::OnUpdateSetBack(CCmdUI* pCmdUI) 
{
	int time = GetSequence()->GetCurrentFrame();
	pCmdUI->Enable(GetSegment()->GetTimeSpan().first != time 
		&& GetSegment()->GetOrigin() >= time);
}

void SegmentCommands::OnSetFront() 
{
	Span t = GetSegment()->GetTimeSpan();
	t.last = GetSequence()->GetCurrentFrame();
	GetSegment()->SetTimeSpan(t);
}

void SegmentCommands::OnUpdateSetFront(CCmdUI* pCmdUI) 
{
	int time = GetSequence()->GetCurrentFrame();
	pCmdUI->Enable(GetSegment()->GetTimeSpan().last != time 
		&& GetSegment()->GetOrigin() < time);
}

} //namespace My
